﻿using Com.FirstSolver.Splash;
using System;
using System.Collections;
using System.Configuration;
using System.Data;

namespace SocketLib
{
    public static class dao
    {
        /* 处理未执行的命令 模板*/
        public static string Getcommond(string sn, out string st, out string commad, out string oxcmd)
        {
            st = "";
            commad = "";
            oxcmd = "";
            string text;



            int mi = Convert.ToInt32(ConfigurationManager.AppSettings["synctime"].ToString());
            string sql1 = String.Format("select Id from lxb_command where   text like 'SetDateTime%' and   sn = '{0}' and addtime >'{1}'", sn, DateTime.Now.AddMinutes(mi * -1).ToString("yyyy-MM-dd HH:mm:ss"));

            if (!MySqlHelper.Exists(sql1))
            {
                text = String.Format("SetDateTime(date=\"{0}\" time=\"{1}\")", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("HH:mm:ss"));

                sql1 = String.Format("INSERT INTO `lxb_command` (`sn`,  `text`, `remark`) VALUES('{0}', '{1}', '系统自动同步时间')", sn, text);
                MySqlHelper.Query(sql1);
            }


            int n = Convert.ToInt32(MySqlHelper.GetSingle(String.Format("select count(1) from lxb_command where status=0 and  sn='{0}'", sn)));
            if (n > 0)
            {
                DataSet ds = MySqlHelper.Query(String.Format("select Id, text from lxb_command where status = 0 and  sn = '{0}' limit 1", sn));

                text = ds.Tables[0].Rows[0]["text"].ToString();
                st = ds.Tables[0].Rows[0]["Id"].ToString();
              



                MySqlHelper.ExecuteSql("update lxb_command set exec=exec + 1 where id=" + st);
                MySqlHelper.ExecuteSql("update lxb_command set isok=0, status=1 where exec >3");
                if (text.StartsWith("SetDateTime("))
                {
                    text = String.Format("SetDateTime(date=\"{0}\" time=\"{1}\")", DateTime.Now.ToString("yyyy-MM-dd"), DateTime.Now.ToString("HH:mm:ss"));
                }

                if (text.Length > 30)
                {
                    commad = text.Substring(0, 30);
                }
                else
                {
                    commad = text;
                }
                oxcmd = commad;

                return text;

            }

            return "";
        }

        /* 处理上传的 模板*/
        public static void UpEmployee(string answer, string sn)
        {
            answer = answer.Replace("Return(result=\"success\"", "Employee(");
            string id = FaceId_Item.GetKeyValue(answer, "id");
            string name = FaceId_Item.GetKeyValue(answer, "name");
            string check_type = FaceId_Item.GetKeyValue(answer, "check_type");
            String sql = string.Format("select Aid  from lxb_employee where id={0} and sn='{1}'", id, sn);
            if (MySqlHelper.Exists(sql))
            {
                sql = string.Format("update  lxb_employee set Name='{0}',check_type='{1}' ,data='{2}' where id={3} and sn='{4}'", name, check_type, answer, id, sn);

            }
            else
            {
                sql = string.Format("insert into  lxb_employee ( Name,check_type,data,id,sn) values('{0}','{1}','{2}','{3}','{4}')", name, check_type, answer, id, sn);

            }
            MySqlHelper.ExecuteSql(sql);
        }

        /* 设置设备不活动自动下线
         *  n 表示几分钟不活动的
         
             */
        public static void SetOffline(int n)
        {

            MySqlHelper.ExecuteSql(string.Format("update  lxb_device set status=0 where lasttime <'{0}'", DateTime.Now.AddMinutes(0 - n).ToString("yyyy-MM-dd HH:mm:ss")));
        }

        /*
         插入打卡记录
             */
        public static bool insertlog(string Answer, String SerialNumber)
        {
            string id = FaceId_Item.GetKeyValue(Answer, "id");
            string name = RegexHelper.getAttr(Answer, "name");
            string time = RegexHelper.getAttr(Answer, "time");
            string authority = RegexHelper.getAttr(Answer, "authority");
            string photo = RegexHelper.getAttr(Answer, "photo");

            String Sql = string.Format("select id from  `lxb_kaoqin_log` where id={0} and  `time`='{1}' and  `sn`='{2}'", id, time, SerialNumber);
            if (MySqlHelper.Exists(Sql))
            {
                return true;
            }

            Sql = string.Format("INSERT INTO `lxb_kaoqin_log` ( `Id`, `UID`, `name`, `time`,`authority`, `photo`,`sn`) VALUES ('{0}', '{1}', '{2}','{3}', '{4}','{5}','{6}');", id, "", name, time, authority, photo, SerialNumber);
            int n = MySqlHelper.ExecuteSql(Sql);

            if (MySqlHelper.Exists("select lxb_user from user where state=2 and user_id= " + id))
            {

                string sbid = Convert.ToString(MySqlHelper.GetSingle("select sbid from lxb_sbid where userid= " + id));
                if (!string.IsNullOrEmpty(sbid))
                {
                    JPushHelper.JPush(sbid, "考勤提醒", name + "" + time + "打卡");
                }
            }
            if (n > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /*
        批量 插入打卡记录
            */
        public static bool batchlogs(string Answer)
        {

            string SerialNumber = RegexHelper.getAttr(Answer, "dev_Id");

            ArrayList id = RegexHelper.getAttrs(Answer, "id");
            ArrayList name = RegexHelper.getAttrs(Answer, "name");
            ArrayList time = RegexHelper.getAttrs(Answer, "time");
            ArrayList authority = RegexHelper.getAttrs(Answer, "authority");
            ArrayList photo = RegexHelper.getAttrs(Answer, "photo");
            string photos = "";
            for (int i = 0; i < id.Count; i++)
            {

                String Sql = string.Format("select id from  `lxb_kaoqin_log` where id={0}  and  `time`='{1}' and `sn`='{2}'", id[i], time[i], SerialNumber);
                if (!MySqlHelper.Exists(Sql))
                {

                    if (photo.Count > (i))
                    {
                        photos = photo[i].ToString();
                    }
                    else
                    {
                        photos = "";
                    }

                    Sql = string.Format("INSERT INTO `lxb_kaoqin_log` ( `Id`, `UID`, `name`, `time`,`authority`, `photo`,`sn`) VALUES ('{0}', '{1}', '{2}','{3}', '{4}','{5}','{6}');", id[i], "", name[i], time[i], authority[i], photos, SerialNumber);
                    int n = MySqlHelper.ExecuteSql(Sql);
                }

            }
            return true;
        }
        /*
        设置设备信息
        */
        public static int SetInfo(String Answer, String SerialNumber)
        {
            string dev_id = RegexHelper.getAttr(Answer, "dev_id");
            string edition = RegexHelper.getAttr(Answer, "edition");
            string time = RegexHelper.getAttr(Answer, "time");
            string ip = RegexHelper.getAttr(Answer, "ip");
            string mac = RegexHelper.getAttr(Answer, "mac");
            string max_faceregist = RegexHelper.getAttr(Answer, "max_faceregist");
            string real_faceregist = RegexHelper.getAttr(Answer, "real_faceregist");
            string max_facerecord = RegexHelper.getAttr(Answer, "max_faceregist");
            string real_facerecord = RegexHelper.getAttr(Answer, "real_facerecord");
            string max_employee = RegexHelper.getAttr(Answer, "max_employee");
            string real_employee = RegexHelper.getAttr(Answer, "real_employee");
            string max_other = RegexHelper.getAttr(Answer, "max_other");
            string real_other = RegexHelper.getAttr(Answer, "real_other");
            string type = RegexHelper.getAttr(Answer, "type");
            string alg_edition = RegexHelper.getAttr(Answer, "alg_edition");

            DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            /* Return(result="success"  time="2017-07-12 22:25:42" 
            dev_id ="8115716090002203" edition="2.240.249" volume="2"
                weigen="" ip="192168001103" gateway="192168001001"
                netmask="255255255000" mac="00:0C:5B:06:98:68" 
                max_managernum="8" managernum="0"
                max_faceregist="400" real_faceregist="1"
                max_facerecord="150000" real_facerecord="6"
                max_employee="10400" real_employee="4" max_other="10000" 
                real_other="3" type="C240" alg_edition="3.1") */


            int i = MySqlHelper.ExecuteSql("update  lxb_device set Type ='" + type + "', Mac='" + mac + "', IP='" + ip + "', edition='" + edition + "',"
                + "info ='" + Answer + "',usernum=" + real_employee + ",usernumtotal=" + max_employee + ",recnum=" + real_facerecord
                + ",recnumtotal =" + max_facerecord + " where sn='" + dev_id + "'");

            return i;


        }
        /*
         设置设备在线
         返回 0 表示设备不存在或者禁用 
         返回 1 表示设备存在而且更新在线成功.
         */
        public static int SetOnline(String SerialNumber)
        {
            int i = 0;
            if (!MySqlHelper.Exists("select  Id from lxb_device where isok=1 and sn='" + SerialNumber + "'"))
            {
                return i;
            }
            else
            {
                MySqlHelper.ExecuteSql(string.Format("update  lxb_device set status=1, lasttime='{0}'", DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")));
                i = 1;
            }
            return i;
            // Return(result = "success"  time = "2017-07-12 22:08:53" dev_id = "8115716090002203" edition = "2.240.249" volume = "2" weigen = "" ip = "192168001103" gateway = "192168001001" netmask = "255255255000" mac = "00:0C:5B:06:98:68"  max_managernum = "8" managernum = "0" max_faceregist = "400" real_faceregist = "1" max_facerecord = "150000" real_facerecord = "6" max_employee = "10400" real_employee = "4" max_other = "10000" real_other = "3" type = "C240" alg_edition = "3.1")

        }

    }
}
